package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsAuditBatch;
import com.caiyi.financial.nirvana.sms.entity.SmsBatchMsg;
import com.caiyi.financial.nirvana.sms.querybean.QueryBatchMsgBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsBatchMsgMapper extends BaseMapper<SmsBatchMsg> {


    @SelectProvider(type = BatchMsgSqlProvider.class, method = "selectBatchMsgListByParams")
    List<SmsAuditBatch> selectBatchMsgListByParams(QueryBatchMsgBean bean);

    @SelectProvider(type = BatchMsgSqlProvider.class, method = "selectAuditByParams")
    List<SmsAuditBatch> selectAuditByParams(QueryBatchMsgBean bean);

    class BatchMsgSqlProvider {
        public String selectBatchMsgListByParams(QueryBatchMsgBean bean) {
            return new SQL() {
                {
                    SELECT("id,batch_id,account,phone_count,notify_url,content,ip,create_time,2 as status");
                    FROM("sms_batch_msg");
                    if (!StringUtils.isEmpty(bean.getBatchId())) {
                        WHERE("batch_id=#{batchId}");
                    }
                    if (!StringUtils.isEmpty(bean.getAccount())) {
                        WHERE("account=#{account}");
                    }
                    if (!StringUtils.isEmpty(bean.getContent())) {
                        WHERE("content LIKE concat('%',#{content},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getUid())) {
                        WHERE("uid=#{uid}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }

        public String selectAuditByParams(QueryBatchMsgBean bean) {
            return new SQL() {
                {
                    SELECT("id,batch_id,account,phone_count,notify_url,content,ip,create_time,status");
                    FROM("sms_audit_batch");
                    if (!StringUtils.isEmpty(bean.getBatchId())) {
                        WHERE("batch_id=#{batchId}");
                    }
                    if (bean.getStatus() != null) {
                        WHERE("status=#{status}");
                    }
                    if (!StringUtils.isEmpty(bean.getAccount())) {
                        WHERE("account=#{account}");
                    }
                    if (!StringUtils.isEmpty(bean.getContent())) {
                        WHERE("content LIKE concat('%',#{content},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getUid())) {
                        WHERE("uid=#{uid}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }
    }
}
